//
//  MainCatalogView.m
//  XRmenu
//
//  Created by shengchao yang on 11-11-25.
//  Copyright 2011年 foxhis. All rights reserved.
//

#import "DishCatalogView.h"
@interface DishCatalogView(Private)
-(void) initCatalogButtons;
@end

@implementation DishCatalogView
@synthesize delegate;
int leftIndex=0,rightIndex=4;//当前显示按钮的左右下标(注意是指pluMenu数组里的下标)

-(id) initWithStyle:(NSString *)stylename ViewName:(NSString *)vname{
    self = [super initWithStyle:stylename DishSubViewName:vname];
    currentMenuIndex=APPSESSION.currentMenuIndex;
    if (self) {  
        //1.判断应该显示多少大类按钮
        for (int i=[APPSESSION.pluMenus count]; i<5; i++) {
            ([self viewWithTag:kTagOfStartBT + i]).hidden=YES;//少于5个大类的时候，隐藏部分按钮
        }
        
        //2.具体界面上显示哪些按钮
        if (currentMenuIndex>=5) {
            leftIndex=currentMenuIndex-4;
            rightIndex=currentMenuIndex;
        }
        else{
            if ([APPSESSION.pluMenus count]>=5) {
                leftIndex=0;
                rightIndex=4;
            }else{
                leftIndex=0;
                rightIndex=[APPSESSION.pluMenus count]-1;
            }
        }
        
        //3.初始化大类按钮
        [self initCatalogButtons];
    }
    return self;
}


//初始化大类的按钮内容
-(void) initCatalogButtons{
    for (int i=leftIndex; i<=rightIndex; i++) {
        if ([self viewWithTag:kTagOfStartBT + i - leftIndex] && i<[APPSESSION.pluMenus count]) {
            UIButton * btn=((UIButton *)[self viewWithTag:kTagOfStartBT + i - leftIndex]);
            [btn setTitle:JsonFiled(((PluMenu *)[APPSESSION.pluMenus objectAtIndex:i]).pmDescripts) forState:UIControlStateNormal];
            
            if (APPSESSION.currentMenuIndex >=leftIndex && APPSESSION.currentMenuIndex <= rightIndex && APPSESSION.currentMenuIndex == i) {
                UIColor *selectedColor = RGBA(185.0, 219.0, 125.0, 254.0/ 255.0f);
                [btn setTitleColor:selectedColor forState:UIControlStateNormal]; 
            }else{
                UIColor *unSelectedColor = RGBA(235.0, 217.0, 185.0, 254.0/ 255.0f);
                [btn setTitleColor:unSelectedColor forState:UIControlStateNormal];     
            }
            
            NSString *imgname=[XRmenuPublic getGeneralSkinImg:@"MainView/CatalogView/bt_catalog_plumenu.png"];
            [btn setBackgroundImage:[UIImage imageWithContentsOfFile:imgname] forState:UIControlStateNormal];          
        }
    }    
    
    //以下是判定是否需要显示左右箭头
    if (rightIndex+1>=[APPSESSION.pluMenus count]) {
        ([self viewWithTag:kTagOfStartBT+6]).hidden=YES;
    }else{
        ([self viewWithTag:kTagOfStartBT+6]).hidden=NO;
    }
    
    if (leftIndex>0) {
        ([self viewWithTag:kTagOfStartBT+5]).hidden=NO;
    }else{
        ([self viewWithTag:kTagOfStartBT+5]).hidden=YES;
    }
}

#pragma mark 重载button处理方法
-(void) buttonClicked:(id)sender{
    NSInteger bid = [sender tag];
    if(bid==kTagOfStartBT + 5){
        DebugLog(@"点击了向左按钮");       
        leftIndex--;
        rightIndex--;
        [self initCatalogButtons];
        [APPSESSION playSoundOnEvent:[XRmenuPublic getGeneralSkinImg:@"buttonClicked.caf"]];
    }else if(bid==kTagOfStartBT + 6){
        DebugLog(@"点击了向右按钮");
        leftIndex++;
        rightIndex++;
        [self initCatalogButtons];
        [APPSESSION playSoundOnEvent:[XRmenuPublic getGeneralSkinImg:@"buttonClicked.caf"]];
    }else{//按下大类按钮触发MainView的方法改变显示的菜品
        NSInteger changedIndex=leftIndex+bid-kTagOfStartBT;     
        if (currentMenuIndex!=changedIndex) {
            DebugLog(@"选中的大类发生了变化");
            [delegate DishCatalogUpdate:changedIndex];//回调MainView里的方法更新界面
            //设置大类名称的选中状态
            @try {
                if (APPSESSION.currentMenuIndex >=leftIndex && APPSESSION.currentMenuIndex <= rightIndex) {
                    DebugLog(@"APPSESSION.currentMenuIndex=%d,leftIndex=%d", APPSESSION.currentMenuIndex, leftIndex);
                    UIButton * btn1=((UIButton *)[self viewWithTag:kTagOfStartBT + APPSESSION.currentMenuIndex - leftIndex]);
                    UIColor *selectedColor = RGBA(185.0, 219.0, 125.0, 254.0/ 255.0f);
                    [btn1 setTitleColor:selectedColor forState:UIControlStateNormal];
                }  
                
                if (APPSESSION.currentMenuIndex >= leftIndex &&APPSESSION.currentMenuIndex <= rightIndex 
                    && currentMenuIndex >= leftIndex && currentMenuIndex <= rightIndex){
                    DebugLog(@"oldMenuIndex=%d,leftIndex=%d", currentMenuIndex , leftIndex);
                    UIButton * btn=((UIButton *)[self viewWithTag:kTagOfStartBT + currentMenuIndex - leftIndex]);
                    UIColor *unSelectedColor = RGBA(235.0, 217.0, 185.0, 254.0/ 255.0f);
                    [btn setTitleColor:unSelectedColor forState:UIControlStateNormal];
                }
            }
            @catch (NSException *exception) {
                DebugLog(@"catch:name=%@; reason=%@", [exception name],[exception reason]);
            }
            @finally {
                DebugLog(@"currentPluIndex=%d", APPSESSION.currentPluIndex);
            }       
            currentMenuIndex=changedIndex;
            [APPSESSION playSoundOnEvent:[XRmenuPublic getGeneralSkinImg:@"buttonClicked.caf"]];
        }else{
            DebugLog(@"选中的大类没有发生变化");
        }
    }
}

#pragma mark -
-(void) dealloc
{
    DebugLog(@"DishCatalogView dealloc!!!");
	self.delegate = nil;
	[super dealloc];
}
@end
